package com.healthcloud.service.impl;

import java.util.Collections;
import java.util.List;

import com.healthcloud.entity.HlHospitalEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.dao.BusInstrumentDao;
import com.healthcloud.dao.SysLogDao;
import com.healthcloud.entity.BusInstrumentEntity;
import com.healthcloud.service.BusInstrumentService;
import com.healthcloud.utils.CommonMethod;
import com.healthcloud.utils.PageParam;

@Service
@Transactional
public class BusInstrumentServiceImpl implements BusInstrumentService {

	@Autowired
	private BusInstrumentDao busInstrumentDao;

	@Autowired
	private SysLogDao sysLogDao;

	@Autowired
	private CommonMethod commonMethod;

	@Override
	public List<BusInstrumentEntity> search(BusInstrumentEntity entity,
			PageParam pageParam) {
		return busInstrumentDao.queryList(entity, pageParam);
	}

	@Override
	public int count(BusInstrumentEntity entity) {
		return busInstrumentDao.queryTotal(entity);
	}

	@Override
	public BusInstrumentEntity searchById(Long id) {

		if (id==null) {
			return null;
		}
		return busInstrumentDao.searchById(id);
	}

	@SysLog("保存设备")
	@Override
	public int createInstr(BusInstrumentEntity entity) {
		if(StringUtils.isBlank(entity.getInstrumentNum())){
			busInstrumentDao.save(entity);
		}else{
			int num = Integer.parseInt(entity.getInstrumentNum());
			for(int i = 0;i<num;i++){
				entity.setInstrumentId(null);
				busInstrumentDao.save(entity);
			}
		}
		return 1;
	}

	@SysLog("修改设备")
	@Override
	public int updateInstr(BusInstrumentEntity entity) {

		int i = busInstrumentDao.update(entity);
		//日志		
		return i;
	}

	@Override
	public int deleteInstr(int id) {

		return busInstrumentDao.deleteInstr(id);
	}

	@Override
	@Transactional
	public int deletes(int[] ids) {
		int j = 0;
		for (int i = 0; i < ids.length; i++) {
			j = deleteInstr(ids[i]);
		}
		return j;
	}

	@Override
	public List<BusInstrumentEntity> searchByName(String instrumentType,
			String offset, String limit,Long hospId) {
		return busInstrumentDao.searchByName(instrumentType, offset, limit,hospId);
	}

	@Override
	public int countByName(String instrumentType, Long hospId) {

		return busInstrumentDao.countByName(instrumentType, hospId);
	}

	@Override
	public List<BusInstrumentEntity> findSNByIdCard(BusInstrumentEntity entity) {

		return busInstrumentDao.findSNByIdCard(entity);
	}

	@Override
	public void createInstr(List<BusInstrumentEntity> instruments) {
		for (int i = 0; i < instruments.size(); i++) {
			createInstr(instruments.get(i));
		}
	}


	@Override
	public List<String> getOpenID(String mKMedicalinstrumentId) {
		return busInstrumentDao.getOpenID(mKMedicalinstrumentId);
	}

	@Override
	public List<BusInstrumentEntity> listByUserId(Long userId) {
		return busInstrumentDao.listByUserId(userId);
	}

	@Override
	public BusInstrumentEntity getBySnAndIdCard(String sn, String idCard) {
		return busInstrumentDao.getBySnAndIdCard(sn, idCard);
	}
}
